/**
 * file
 */
package com.tdkj.action.backstage.admin;

import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.tdkj.action.BaseAction;
import com.tdkj.model.user.BeanMessage;
import com.tdkj.model.user.BeanUser;
import com.tdkj.model.user.BeanUserLevel;
import com.tdkj.service.user.IBeanUserLoginLogService;

/**
 * 
 * FileName: WebSiteUserAction.java
 * 
 * @Description: TODO网站用户管理
 * 
 *               All rights Reserved, Designed By ZTE-ITS
 * 
 *               Copyright: Copyright(C) 2010-2011
 * 
 *               Company 天成科技有限公司
 * 
 * @author: 李兴建
 * 
 * @version V1.0
 * 
 *          Createdate: 2012-7-22 下午04:00:31
 * 
 * 
 * 
 *          Modification History:
 * 
 *          Date Author Version Discription
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 *          
 *          ----------------------------------------------------------------------
 *          -------------
 * 
 *          2012-7-22 李兴建 1.0 1.0
 * 
 *          Why & What is modified: <修改原因描述>
 */
public class WebSiteUserAction extends BaseAction {
	private static final long serialVersionUID = -5400336898695398623L;
	private BeanMessage beanPublishWebsite;
	private String optionID;
	private BeanUser beanUser;
	private String[] ids;
	private List<BeanUserLevel> beanUserLevels;
	private String bserLevelID;

	private IBeanUserLoginLogService userLoginLogService;

	@Resource
	public void setUserLoginLogService(IBeanUserLoginLogService userLoginLogService) {
		this.userLoginLogService = userLoginLogService;
	}

	public String userManage() {
		Criteria criteria = this.createCriteria(BeanUser.class);
		criteria.addOrder(Order.desc("registDateTime"));// 对状态进行排序
		// criteria.add(Restrictions.eq("beanState", 0));
		this.queryByPage(criteria, "webSiteUserAction!userManage.action", 10);
		return "userManage";
	}

	/**
	 * 恢复已删除用户
	 * 
	 * @return
	 */
	public String recovery() {
		BeanUser beanUsertemp = this.getBaseService().findById(Long.parseLong(optionID), BeanUser.class);
		if (beanUsertemp.getBeanState() == 1) {
			beanUsertemp.setBeanState(0);
			this.getBaseService().save(beanUsertemp);
			this.adminLogWrite("恢复用户状态   ,用户ID:" + beanUsertemp.getId() + " 恢复");
		}
		return userManage();
	}

	/**
	 * 修改用户的信息
	 * 
	 * @return
	 */
	public String modifyUser() {

		return "modifyUser";
	}

	/**
	 * 修改用户状态为冻结
	 * 
	 * @return
	 */
	public String modifyDJ() {
		BeanUser beanUsertemp = this.getBaseService().findById(Long.parseLong(optionID), BeanUser.class);
		beanUsertemp.setState(1);
		this.getBaseService().save(beanUsertemp);
		this.adminLogWrite("修改用户状态   ,用户ID:" + beanUsertemp.getId() + " 修改为冻结");
		return userManage();
	}

	/**
	 * 修改用户状态为正常
	 * 
	 * @return
	 */
	public String modifyZC() {
		BeanUser beanUsertemp = this.getBaseService().findById(Long.parseLong(optionID), BeanUser.class);
		beanUsertemp.setState(0);
		this.getBaseService().save(beanUsertemp);
		this.adminLogWrite("修改用户状态   ,用户ID:" + beanUsertemp.getId() + " 修改为正常");
		return userManage();
	}

	/**
	 * 设置为专家
	 * 
	 * @return
	 */
	public String modifyZJ() {
		BeanUser beanUsertemp = this.getBaseService().findById(Long.parseLong(optionID), BeanUser.class);
		beanUsertemp.setExpert(true);
		this.getBaseService().save(beanUsertemp);
		this.adminLogWrite("修改用户状态   ,用户ID:" + beanUsertemp.getId() + "  设置为专家");
		return userManage();

	}

	/**
	 * 认证通过
	 * 
	 * @return
	 */
	public String modifyRZTG() {
		BeanUser beanUsertemp = this.getBaseService().findById(Long.parseLong(optionID), BeanUser.class);
		beanUsertemp.setCertification(true);
		this.getBaseService().save(beanUsertemp);
		this.adminLogWrite("修改用户状态   ,用户ID:" + beanUsertemp.getId() + "  认证通过");
		return userManage();

	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	public String delete() {
		BeanUser beanUsertemp = this.getBaseService().findById(Long.parseLong(optionID), BeanUser.class);
		beanUsertemp.setBeanState(1);
		this.getBaseService().saveOrUpdate(beanUsertemp, BeanUser.class);
		this.adminLogWrite("删除用户   ,用户ID:" + beanUsertemp.getId());
		return userManage();
	}

	public String deleteAll() {
		if (null != ids) {
			for (String idtemp : ids) {
				BeanUser beanUsertemp = this.getBaseService().findById(Long.parseLong(idtemp), BeanUser.class);
				beanUsertemp.setBeanState(1);
				this.getBaseService().saveOrUpdate(beanUsertemp, BeanUser.class);
				this.adminLogWrite("删除用户   ,用户ID:" + beanUsertemp.getId());
			}
		}
		return userManage();
	}

	/**
	 * 修改用户信息
	 * 
	 * @return
	 */
	public String modifyUserInfo() {
		beanUser = this.getBaseService().findById(Long.parseLong(optionID), BeanUser.class);
		this.getReturnInfo().put("lastLoginLog", userLoginLogService.getLastLoginLog(Long.parseLong(optionID)));
		return "modifyUserInfo";
	}

	/**
	 * 发布站内信页面
	 * 
	 * @return
	 */
	public String publishPage() {
		beanPublishWebsite = new BeanMessage();
		beanUserLevels = this.getBaseService().findAll(BeanUserLevel.class);
		return "publishPage";
	}

	/**
	 * 发布
	 * 
	 * @return
	 */
	public String publish() {
		if (bserLevelID.equals("-1")) {
			beanPublishWebsite.setAllUser(true);
		} else {
			BeanUserLevel beanUserLevel = this.getBaseService().findById(Long.parseLong(bserLevelID), BeanUserLevel.class);
			beanPublishWebsite.setBeanUserLevel(beanUserLevel);
		}
		beanPublishWebsite.setCreatetime(new Timestamp(System.currentTimeMillis()));
		beanPublishWebsite.setBeanAdmin(this.getCurrentAdmin());
		this.getBaseService().save(beanPublishWebsite);
		return userManage();
	}

	public String getOptionID() {
		return optionID;
	}

	public void setOptionID(String optionID) {
		this.optionID = optionID;
	}

	public BeanUser getBeanUser() {
		return beanUser;
	}

	public void setBeanUser(BeanUser beanUser) {
		this.beanUser = beanUser;
	}

	public String[] getIds() {
		return ids;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}

	/**
	 * @return the beanPublishWebsite
	 */
	public BeanMessage getBeanPublishWebsite() {
		return beanPublishWebsite;
	}

	/**
	 * @param beanPublishWebsite
	 *            the beanPublishWebsite to set
	 */
	public void setBeanPublishWebsite(BeanMessage beanPublishWebsite) {
		this.beanPublishWebsite = beanPublishWebsite;
	}

	/**
	 * @return the serialversionuid
	 */
	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public List<BeanUserLevel> getBeanUserLevels() {
		return beanUserLevels;
	}

	public void setBeanUserLevels(List<BeanUserLevel> beanUserLevels) {
		this.beanUserLevels = beanUserLevels;
	}

	public String getBserLevelID() {
		return bserLevelID;
	}

	public void setBserLevelID(String bserLevelID) {
		this.bserLevelID = bserLevelID;
	}
}
